var app = getApp();

var user = require('../../../../util/user.js');
var util = require('../../../../util/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    prodId: 0,
    order:{
      id:0,
      orderNo:"",
      userId:0,
      userName:"",
      prodName:"",
      orderPrice:"",
      number:0,
      orderStatus:0,
      createdAt:""
    }
  },
  hasLogin:false,
  flag: false,


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      prodId: options.prod_id
    });
    if (!app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/page/auth/login/login"
      });
    }
  },
  order: function (url, token) {
    var that = this;
    wx.request({
      url: url,
      method: 'GET',
      header: {
        "Content-Type": "json",
        'X-AUTH-TOKEN': token
      },
      success: function (res) {
        console.log(res.data);    //控制台打印通过API获取的object对象，可展开查看结构
        that.setData({
          order: res.data
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.hasLogin) {
      var token = wx.getStorageSync('token');
      this.setData({
        hasLogin: true,
      });

      var that = this;  //把this对象复制到临时变量that
      var ordertUrl = app.globalData.apiBase + app.globalData.orderUrl + "?prod_id=" + this.data.prodId;
      console.log(ordertUrl);
      this.order(ordertUrl, token);
    } else {
      wx.removeStorageSync('userInfo');
      wx.removeStorageSync('token');
    }
  },
  // “去付款”按钮点击效果
  payOrder: function() {
    let that = this;
    var prepayUrl = app.globalData.apiBase + app.globalData.prepayUrl;
    util.request(prepayUrl, {
      orderId: that.data.order.id
    }, 'POST').then(function(res) {
      if (res.retCode === 0) {
        const payParam = res.retData;
        console.log("repay:"+payParam.timeStamp+",nonceStr="+payParam.nonceStr+",package="+payParam.packageValue+",signType="+ payParam.signType+",paySign="+payParam.paySign);
        console.log("支付过程开始");
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.packageValue,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function(res) {
            console.log("支付过程成功");
            util.redirect('/page/order/pages/list/list');
          },
          'fail': function(res) {
            console.log("支付过程失败");
            util.showErrorToast('支付失败');
          },
          'complete': function(res) {
            console.log("支付过程结束")
          }
        });
      }
    });

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})